require 'extract/rules/rule'
require 'csv'


module Extract
  
  class XMLRuleMulti < XMLRule
    
    def extract source_receiver, dest_receiver
      matchs = REXML::XPath.match source_receiver, xpath
      
      vals = []
      matchs.each { |m|
        v = m.text
        if !v.nil? then
          preproces_val v
          vals << v
        end
      }
      
      if vals.size >= 1 then
        val = ""
        CSV::Writer.generate(val) { |csv| csv << vals }
        handle_val val, dest_receiver
      end
      
      #      if matchs.length == 1 then
      #        val = matchs[0].text if matchs[0].instance_of? REXML::Element
      #        val = matchs[0].value if matchs[0].instance_of? REXML::Attribute
      #        val = matchs[0].string if matchs[0].instance_of? REXML::Comment
      #        preproces_val val
      #        handle_val val, dest_receiver
      #      end
    end
    
    def preproces_val(val)
      val.strip!
      val.downcase!
    end
    
  end
  
end